package com.yu.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.junit.Test;

/**
 * @author yushijun
 * @date 2018/6/15
 */
public class ParallelStream {

    @Test
    public void testParallelStream() {
//        while (true){
            test();
        testParallel();
//        }

    }

    public void test(){
        List<Double> list = new ArrayList<Double>();
        for (int i = 0; i < 10000000; i++) {
            double d = Math.random() * 1000;
            list.add(d);
        }
        long start = System.nanoTime();
        list = list.stream().sequential().sorted().collect(Collectors.toList());
        long end = System.nanoTime();
        long seqTime = TimeUnit.NANOSECONDS.toMillis(end - start);// 得到串行排序所用的时间
        System.out.println(";串行时间:" + seqTime);
    }


    public void testParallel(){
        List<Double> list = new ArrayList<Double>();
        for (int i = 0; i < 10000000; i++) {
            double d = Math.random() * 1000;
            list.add(d);
        }
        long start = System.nanoTime();
        list = list.stream().parallel().sorted().collect(Collectors.toList());
        long parTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);// 得到并行排序所用的时间
        System.out.println("并行时间:" + parTime);
    }

}
